<?php
include_once('include/_config.inc.php');
include_once('include/_connect.inc.php');
?>
<link rel="stylesheet" type="text/css" href="scripts/katstyle.css">

<b>Kundat -> CMS your Live</b>
</td>
</tr>


<tr bgcolor="#ffffff">
<td colspan=2>
<?php include ("include/adminmenu.php"); ?></td></tr>

<?php

$benutzerid=$_SESSION['logged'];
// Breitemenu anpassen
if (isset($_POST['submitbreite'])) 
{
	$sql = "UPDATE user SET  breitemenu='".mysqli_real_escape_string($conn,$_POST['breite'])."' WHERE id='$benutzerid'";
	$result = mysqli_query($conn,$sql);
	echo $sql; 
}
// Ende Breitemneu anpassen

//Breite auslesen
$query = "Select breitemenu from user where id=$benutzerid";
echo $query;
$result = mysqli_query($conn, $query) or die ("MySQL-Fehler: " . mysqli_error());
while($myrow = mysqli_fetch_assoc($result))
{
		$breitemenu=$myrow['breitemenu'];
}
//Ende Breite auslesen

?>
<tr  bgcolor="#ffffff"><td width=<?php echo $breitemenu; ?>% valin="top">
Menupunkte Konfigurieren<br>

 <form action="" method="post" >[Breite in <input name="breite" type="text" size="5" maxlength="5" value="<?php echo $breitemenu; ?>"><b>%</b><input type="submit" name="submitbreite" value="Anpassen"> ]</form>

<?php
// Kat einf&uuml;gen
$x = isset($_POST['x']) ? $_POST['x'] : "";
if ($x=='') { $x=0; }
if (isset($_POST['submitkat']))
{
$result = mysqli_query($conn,"INSERT INTO menu (name, parentId) 
VALUES('".$_POST['katname']."','$x')");
echo $result;
echo "Datensatz hinzugef&uuml;gt";
}

//Kat einf&uuml;gen Ende

//Kategorie &auml;ndern:

if (isset($_POST['submitedit'])) 
{
$sql = "UPDATE menu SET  name='".$_POST['katname']."' WHERE id='$x'";
$result = mysqli_query($conn,$sql);
echo $sql;

echo "Kat ge&auml;ndert";
}

//ENDE Kat &auml;ndern

//Kat L&ouml;schen
//$clear = isset($_GET['clear']) ? $_GET['clear'] : "";
//if($clear) 
	//echo $clear;
if (isset($_GET['clear'])) 
{
	$clear = isset($_GET['clear']) ? $_GET['clear'] : "";
//ERstmal &uuml;berpr&uuml;fen ob Subkategorien angelegt sind

$result = mysqli_query($conn,"SELECT * FROM menu WHERE parentid=$clear");
$anzahl=mysqli_num_rows($result);
//echo $anzahl; 
if ($anzahl!=0)
{ echo "<b>!!!Bitte erst Unterkategorien löschen!!!</b>"; } else {
mysqli_query($conn,"DELETE FROM menu WHERE id=$clear"); echo "Menupunkt gelöscht!";
}
}
//Kat L&ouml;schen ENDE

//Reihenfolge

if (isset($_POST['submitreihe'])) 
{
$reihe = isset($_POST['reihe']) ? $_POST['reihe'] : "";
$sql="UPDATE menu SET reihe='".$_POST['reihe']."' WHERE id='$x'";
mysqli_query($conn,$sql); 
echo $sql;
echo "Reihenfolge ge&auml;ndert";
}

//ENDE Reihenfolge

//Hauptkategorie erstellen
?>
<p><form method="post" action="">
<input name="katname" type="text" size="20" maxlength="40">
<input name="submitkat" class="Button" type="submit" value=" Hauptkategorie anlegen "></p></form>
<?php
// get all menuitems with 1 query
$result = mysqli_query($conn,"
    SELECT
        id, parentId, name, reihe
    FROM
        menu
    ORDER BY
        reihe, parentId, name
");
?>

<style>

ul { margin-left: 0.5em; padding-left: 0.5em; }

</style>

<?php
// prepare special array with parent-child relations
$menuData = array(
    'items' => array(),
    'parents' => array()
);

while ($menuItem = mysqli_fetch_assoc($result))
{
    $menuData['items'][$menuItem['id']] = $menuItem;
    $menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}
?>

<?php
// menu builder function, parentId 0 is the root
function buildMenu($parentId, $menuData)
{
    $html = '';

    if (isset($menuData['parents'][$parentId]))
    {
        $html = '<ul>';
        foreach ($menuData['parents'][$parentId] as $itemId)
        {
        //&uuml;berpr&uuml;fen ob Datensatz editiert...
        $edit = isset($_GET['edit']) ? $_GET['edit'] : "";
        $id=$menuData['items'][$itemId]['id'];
        
          if($id==$edit)
          {
          $html .='<li><form name="meins" method="post" action="">
                   <input  style="background-color: #FFCC66;" name="katname" type="text" size="20" maxlength="100" value="' . $menuData['items'][$itemId]['name'].'">
                   <input name="x" type="hidden" value="'. $menuData['items'][$itemId]['id'].'">
                   <input name="submitedit" class="Button" type="submit" value=" Kategorie bearbeiten "></p></form>
                   ';
          }


        
        //ENDE &Uuml;berpr&uuml;fung
        else
        {
            $html .= '<li >' . $menuData['items'][$itemId]['name'];
        }
                      
             //Untertkat
           //  $html .= '<br>' . $menuData['items'][$itemId]['id'];
             
             
             $html .= '[<a title="Kategorie l&ouml;schen" href="?clear='. $menuData['items'][$itemId]['id'].'" onClick="return confirm("Wirklich Löschen?")"><span>L&ouml;schen</span></a>]';
			$html .= '[<a title="Kategorie bearbeiten" href="?edit='. $menuData['items'][$itemId]['id'].'&contentname='. $menuData['items'][$itemId]['name'].'">Bearbeiten</a>]';
			$html .= '[<a title="Conetnt zum Menupunkt" href="?contentid='. $menuData['items'][$itemId]['id'].'&contentname='. $menuData['items'][$itemId]['name'].'">+Content</a>]';
			

            $html .= '<p><form method="post" action="">
                      <input name="katname" type="text" size="20" maxlength="40">
                      <input name="x" type="hidden" value="'. $menuData['items'][$itemId]['id'].'">

                      <input name="submitkat" class="Button" type="submit" value=" Unterkategorie anlegen "></p></form>';
                      

            //UnterKat ENDE
            
           // Reihenfolge
           
           
           $html .= '<form name="meins" method="post" action="">
<input name="reihe" type="text" size="5" maxlength="5" value="'.$menuData['items'][$itemId]['reihe'].'">
<input name="x" type="hidden" value="'. $menuData['items'][$itemId]['id'].'">
<input name="submitreihe" type="submit" value="&Auml;ndern"></form>';


           //ENDE Reihenfolge


            // find childitems recursively
            $html .= buildMenu($itemId, $menuData);

            $html .= '</li>';
        }
        $html .= '</ul>';
    }

    return $html;
}

// output the menu
echo buildMenu(0, $menuData);
?>

</td>
<td valign="top">

<?php include_once("include/index-content.php"); ?>

</td>
</tr>